<?php
/*
 * Created on 2011-1-16
 * by lixilei
 */

class RenRenMobileBiz {
	
	/**
	 * 获取人人网的影响力
	 */
	public function getWeight($user_id){
		$renrenDB = spClass("RenRenModel", null, MODEL_PATH."RenRenModel.php");
		$result = $renrenDB->find(array("user_id"=>$user_id), NULL, "weight");
		return $result["weight"];
	}
	
	/**
	 * 初次设定人人网同步
	 */
	public function setConnectionInfo($user_id, $email, $password){
		// 获取网络信息
		$socialNetworkInfo = $this->getSocialNetworkInfo($email, $password);
		if($socialNetworkInfo["ret"] == _SUCCESS){
			// 帐号可用，将获取的信息写入数据库
			return $this->saveConnection($user_id, $socialNetworkInfo["friends"], $socialNetworkInfo["views"], $socialNetworkInfo["weight"], $email, $password);
		} else{
			return $socialNetworkInfo;
		}
	}
	
	/**
	 * 取消同步
	 */
	public function disconnect($user_id){
		// 删除数据库项
		$userDB = spClass("UserModel", null, MODEL_PATH."UserModel.php");
		// 开启事务
		$userDB->beginTransaction();
		$renrenDB = spClass("RenRenModel", null, MODEL_PATH."RenRenModel.php");
		// 取消同步标识
		$conditions = array(
			"user_id" => $user_id,
		);
		$result = $renrenDB->find(array("user_id"=>$user_id), NULL, "weight");
		if(!$result){
			// 回滚事务
			$userDB->rollback();
			return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);
		}
		$result = $userDB->update($conditions, array("renren_connect"=>0), "weight=weight-".$result["weight"]);
		if(!$result){
			// 回滚事务
			$userDB->rollback();
			return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);
		}
		// 删除同步信息
		$result = $renrenDB->delete($conditions);
		if($result){
			// 提交事务
			$userDB->commit();
			return array("ret"=>_SUCCESS);
		}
		// 回滚事务
		$userDB->rollback();
		return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);
	}
	
	/**
	 * 发布消息
	 */
	public function publish($user_id, $msg){
		// 查询帐号信息
		$renrenDB = spClass("RenRenModel", null, MODEL_PATH."RenRenModel.php");
		$conditions = array(
			"user_id" => $user_id
		);
		$result = $renrenDB->find($conditions);
		if(!$result){
			return array("ret"=>_FAIL, "errMsg"=>_RENREN_CONNECTION_NOT_EXIST);
		}
		
		// 发布信息
		require_once(LIB_PATH."snoopy.class.php");
		$snoopy = new Snoopy();		
		$submit_vars = array(
			"email" => $result["email"],
			"password" => $result["password"],
			"origURL" => "/profile.do",
		);
		$this->mask(&$snoopy);
		if($snoopy->submit("http://3g.renren.com/profile.do", $submit_vars)){
			// 信息错误，登录失败
			if(preg_match('/<div class="error"><b>/', $snoopy->results) == 1){
				return array("ret"=>_FAIL, "errMsg"=>_RENREN_LOGIN_ERROR);
			}
			// 登录成功
			if(preg_match('/<div class="sec nav">/', $snoopy->results) == 1){
				$tags = preg_split('/<span class="gray">\(|\)<\/span><br \/></', $snoopy->results);
				$viewsCount = $tags[1];
				$friendsCount = $tags[3];
				$tags = preg_split('/<form action="|" method="post">/', $snoopy->results);
				$submit_vars = array(
					"sour" => "profile",
					"status" => $msg,
					"update" => "发布",
				);
				$this->mask(&$snoopy);
				$snoopy->submit($tags[1], $submit_vars);
				if(preg_match('/<div class="notice_y">/', $snoopy->results) == 1){
					$result = $this->saveConnection($user_id, $friendsCount, $viewsCount, $this->calculateWeight($friendsCount, $viewsCount));
					if($result["ret"] == _FAIL){
						// TODO 保存同步时刷新信息的错误
					}
					return array("ret"=>_SUCCESS);
				}
				return array("ret"=>_FAIL, "errMsg"=>_RENREN_PUBLISH_ERROR);
			}
		} else{
			return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);
		}
	}
	
	/**
	 * 获取网络信息
	 */
	private function getSocialNetworkInfo($email, $password){
		require_once(LIB_PATH."snoopy.class.php");
		$snoopy = new Snoopy();		
		$submit_vars = array(
			"email" => $email,
			"password" => $password,
			"origURL" => "/profile.do",
		);		
		$this->mask(&$snoopy);
		if($snoopy->submit("http://3g.renren.com/profile.do", $submit_vars)){
			// 信息错误，登录失败
			if(preg_match('/<div class="error"><b>/', $snoopy->results) == 1){
				return array("ret"=>_FAIL, "errMsg"=>_RENREN_LOGIN_ERROR);
			}
			// 登录成功
			if(preg_match('/<div class="sec nav">/', $snoopy->results) == 1){
				$tags = preg_split('/<span class="gray">\(|\)<\/span><br \/></', $snoopy->results);
				$viewsCount = $tags[1];
				$friendsCount = $tags[3];
				return array(
							"ret"=>_SUCCESS,
							"friends"=>$friendsCount,
							"views"=>$viewsCount,
							"weight"=>$this->calculateWeight($friendsCount, $viewsCount)
						);
			}
		} else{
			return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);
		}
	}
	
	/**
	 * 计算人人网影响力
	 */
	private function calculateWeight($friendsCount, $viewsCount){
		return round($friendsCount*0.8 + $viewsCount*0.2);
	}
	
	/**
	 * 保存同步得来的信息
	 */
	private function saveConnection($user_id, $friendsCount, $viewsCount, $weight, $email=NULL, $password=NULL){
		// 将信息写入数据库
		$userDB = spClass("UserModel", null, MODEL_PATH."UserModel.php");
		$conditions = array(
			"user_id" => $user_id
		);
		$result = $userDB->find($conditions, null, "renren_connect");
		if($result["renren_connect"] == 0 && $email != NULL && $password != NULL){
			// 首次同步信息，开启事务
			$userDB->beginTransaction();
			$renrenDB = spClass("RenRenModel", null, MODEL_PATH."RenRenModel.php");
			// 添加同步标识
			$result = $userDB->update($conditions, array("renren_connect"=>1), "weight=weight+".$weight);
			if(!$result){
				// 回滚事务
				$userDB->rollback();
				return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);
			}
			// 设定同步信息
			$row = array(
				"user_id" => $user_id,
				"email" => $email,
				"password" => $password,
				"friends_num" => $friendsCount,
				"views_num" => $viewsCount,
				"weight" => $weight
			);
			$result = $renrenDB->create($row);
			if($result){
				// 提交事务
				$userDB->commit();
				return array("ret"=>_SUCCESS);
			}
			// 回滚事务
			$userDB->rollback();
			return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);	
		} else{
			// 更新已有的同步信息
			$renrenDB = spClass("RenRenModel", null, MODEL_PATH."RenRenModel.php");
			$row = array(
				"friends_num" => $friendsCount,
				"views_num" => $viewsCount,
				"weight" => $weight
			);
			$result = $renrenDB->update($conditions, $row);
			if($result){
				return array("ret"=>_SUCCESS);
			}
			return array("ret"=>_FAIL, "errMsg"=>_UNKNOW_ERROR);	
		}
	}
	
	/**
	 * 伪装
	 */
	private function mask($snoopy){
//		$snoopy->cookies["PHPSESSID"] = "fc106b1918bd522cc863f36890e6fff7"; //伪装sessionid
//		$snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"; //伪装浏览器
//		$snoopy->referer = "http://www.only4.cn"; //伪装来源页地址 http_referer
//		$snoopy->rawheaders["Pragma"] = "no-cache"; //cache 的http头信息
//		$snoopy->rawheaders["X_FORWARDED_FOR"] = "127.0.0.1"; // 伪装ip
	}
}
?>
